<!DOCTYPE html>
<html lang="en" class="no-js" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout" layout:decorator="layout">
<head>
<title>添加包邮活动管理</title>
<script type="text/javascript">
 $(document).ready(function(){
	
	if($("#single").val()=="SINGLEGOOD"){
		 $("#hidden_orderPrice").hide();
		 $("#hidden_brandName").hide();
		 $("#hidden_single_div").show();
	}else if($("#single").val()=="BRAND"){
		$("#hidden_single_div").hide();
		$("#hidden_orderPrice").hide();
		$("#hidden_brandName").show();
	}
	else{
		$("#hidden_single_div").hide();
		$("#hidden_orderPrice").show();
		$("#hidden_brandName").hide();
	}
	
	$('#treebutton').html($("#select_goods_CategoryId").find("option:selected").text() + '<i class="fa fa-angle-down"></i>');
	$("#select_goods_CategoryId").change(function(){$('#treebutton').html($("#select_goods_CategoryId").find("option:selected").text() + '<i class="fa fa-angle-down"></i>'); });
}); 


	

	
	
$(document).ready(function(){
	$("#form_postForFreeEdit").validate({
    rules:{
    	postForFreeName:{  //包邮名称不能超过 100个字符
            required:true,
            maxlength:100
        },
        postForFreeType:{  
        	required:true,  
        },
        
        orderPrice:{
        	required:true,
            number:true,
            min:1,
            max:99999999
        	
        },
         brandsIdList:{
        	required:true,
        }, 
        startDate:{  
           required:true,
        },
        endDate:{ 
           required:true,            
        },
        postForFreeInstruction:{
        	 maxlength:300
        }
    },
    messages:{
    	postForFreeName:{
            required:"请输包邮名称",
            maxlength:"最大不能超过100个字符"
        },
        postForFreeType:{
            required:"请选择包邮类型",
        },
        
        orderPrice:{
        	 required:"请输入订单金额",
             number:"请输入正确的订单金额",
             min:"订单金额不能小于等于0",
             max: "订单金额不能大于99999999"
        },
         brandsIdList:{
        	required:"请选择商品品牌",
        }, 
        startDate:{
            required:"请选择开始日期",
        },
        endDate:{
            required:"请选择结束日期",
        },
        postForFreeInstruction:{
            maxlength:"最大不能超过300个字符"
        }
    }
});
})
</script>
</head>
<body>
<div layout:fragment="content">
<link th:href="@{/plugins/bootstrap-modal/css/bootstrap-modal-bs3patch.css}" rel="stylesheet" href="plugins/bootstrap-modal/css/bootstrap-modal-bs3patch.css"/>
<link th:href="@{/plugins/bootstrap-modal/css/bootstrap-modal.css}" rel="stylesheet" href="plugins/bootstrap-modal/css/bootstrap-modal.css"/>
<link th:href="@{/plugins/zTree/css/zTreeStyle/zTreeStyle.css}" rel="stylesheet" href="plugins/zTree/css/zTreeStyle/zTreeStyle.css"/>
<script th:src="@{/plugins/zTree/js/jquery.ztree.core-3.5.js}" src="plugins/zTree/js/jquery.ztree.core-3.5.js" type="text/javascript"></script>

    <input id="single" type="hidden" th:value="${hidden_divOrderPrice}"/>
	<div class="row">
		<div class="col-md-12">
			<div class="btn-group pull-right">
				<a href="index.html" th:href="@{/promotion/postforfreeactivity/index/}" id="sample_editable_1_new" class="btn green">返回<i class="fa fa-arrow-circle-o-left"></i></a>
			</div>
		</div>
	</div>
	 
	<div class="row">
		<div class="col-md-12">
			<div class="form">
			
				<form role="form"  id="form_postForFreeEdit" th:action="@{/promotion/postforfreeactivity/save}" th:object="${PostForFreeActivityInfo}" method="post" class="form-horizontal" enctype="multipart/form-data">
					<h4 class="form-section">添加包邮活动</h4>
					 <input type="hidden" th:field="*{id}" id="postForFreeId"/> 
					<div class="form-body">
						
						<div class="form-group" th:class="${#fields.hasErrors('postForFreeName')}? 'form-group has-error' : 'form-group'">
							<label class="control-label col-md-3">包邮名称<span class="required" aria-required="true">* </span></label>							
							<div class="col-md-4">
								<input type="text" class="form-control"  id="name" name="name" placeholder="包邮名称" th:field="*{postForFreeName}"/>
								<span class="help-block" th:if="${#fields.hasErrors('postForFreeName')}" th:errors="*{postForFreeName}">包邮名称不能为空</span>
							</div>
						</div>	

				 		<div class="form-group" th:class="${#fields.hasErrors('postForFreeType')}? 'form-group has-error' : 'form-group'" >
							<label class="control-label col-md-3">包邮类型<span class="required" aria-required="true">*</span></label>
							<div class="col-md-4">
								<div class="radio-list" style="width:420px">
									<label class="radio-inline"><span><input type="radio" name="postForFreeType"  id="optionsRadios4" value="SINGLEGOOD"   th:field="*{postForFreeType}" /></span>单品限时包邮</label>
									<label class="radio-inline"><span><input type="radio" name="postForFreeType" id="optionsRadios5" value="ORDER"   checked="checked"  th:field="*{postForFreeType}" /></span> 全场限时包邮</label>
								    <label class="radio-inline"><span><input type="radio" name="postForFreeType" id="optionsRadios6" value="BRAND"  th:field="*{postForFreeType}" /></span> 品牌全场限时包邮</label>
								</div>						
							</div>
						</div>		
				 															
						<div id="hidden_orderPrice" class="form-group" th:class="${#fields.hasErrors('orderPrice')}? 'form-group has-error' : 'form-group'">
							<label class="control-label col-md-3">订单金额<span class="required" aria-required="true">* </span></label>
							<div class="col-md-4">
								<input type="text" class="form-control" placeholder="订单金额"  th:field="*{orderPrice}"  id="orderPrice" name="orderPrice" />
								<span class="help-block" th:if="${#fields.hasErrors('orderPrice')}" th:errors="*{orderPrice}">订单金额不能为空</span>
							</div>
						</div>
						
						<!-- <div id="hidden_brandName" class="form-group"> -->
					    <div id="hidden_brandName" class="form-group" th:class="${#fields.hasErrors('brandsIdList')}? 'form-group has-error' : 'form-group'">
							<label class="control-label col-md-3">商品品牌<span class="required" aria-required="true">* </span></label>
							<div class="col-md-4">
								<div class="checkbox-list">
									<label class="checkbox-inline" th:each="brandval : ${brandList}"> 
										<span>
										   <input type="checkbox" th:field="*{brandsIdList}" th:checked="${brandval.id}" name="brandIds" th:value="${brandval.id}"  th:text="${brandval.name}" id="brandIds"/>
										</span>
								    </label>
								 </div>
							</div>
						</div>
						
						
						<div class="form-group" th:class="${#fields.hasErrors('startDate')}? 'form-group has-error' : 'form-group'">
                            <label class="control-label col-md-3">开始时间<span class="required" aria-required="true">* </span></label>
                            <div class="col-md-4">
                                <input onBlur="checkDate();" id="text_startDate" th:field="*{startDate}" class="form-control" type="text"></input>
                                <span class="help-block" th:if="${#fields.hasErrors('startDate')}" th:errors="*{startDate}">请输入开始时间</span>
                            </div>
						</div>
						
						<div class="form-group" th:class="${#fields.hasErrors('endDate')}? 'form-group has-error' : 'form-group'">
						    <label class="control-label col-md-3">结束时间<span class="required" aria-required="true">* </span></label>
							<div class="col-md-4">
								<input onBlur="checkDate();" id="text_endDate" th:field="*{endDate}" data-format="yyyy-MM-dd hh:mm:ss" class="form-control" type="text"></input>
								<span class="help-block" th:if="${#fields.hasErrors('endDate')}" th:errors="*{endDate}">请输入结束时间</span>
							</div>
						</div>

						<div class="form-group">
							<label class="control-label col-md-3">包邮说明 <span class="required" aria-required="true"></span></label>
							<div class="col-md-4">
								<textarea class="form-control" rows="3" placeholder="包邮说明" id="textarea_instruction" name="textarea_instruction"  th:field="*{postForFreeInstruction}"></textarea>
							</div>
						</div>	
						
						 
					<!-- 	<input type="hidden" id="catIdSearch" th:field="*{categoryId}"/>
						<input type="hidden" id="brandIdSearch" th:field="*{brandId}"/>
						<input type="hidden" id="goodSnSearch" th:field="*{goodSn}" />
						<input type="hidden" id="keyWordSearch" th:field="*{keyword}"/> -->
						<input type="hidden" id="goodsListAdd" th:field="*{goodsIdArr}" />			 
					</div>  
                   
					 <div class="form-actions fluid" >  <!--  style="display: none;" -->
                            <div class="col-md-offset-3 col-md-9">
                                <button id="btn_form_submit" type="submit" class="btn blue">提交</button>
                            </div>
                    </div>
			</form>		
	
<div id="hidden_single_div">
    <div class="row" th:with="selectedGoods=${PostForFreeActivityInfo.postForFreeSingleGoods},selectableGoods=${selectableGoods}">
        <div class="col-md-12" th:fragment="search_form">
	        <div class="form">
	            <form onsubmit="goodsMatchSearchF();return false;" class="form-inline" method="post" th:object="${searchCondition}" name="form_goods_search" id="form_goods_search">
	
	               <!--  <div class="form-group">
	                    <select id="select_goods_CategoryId" th:field="*{categoryId}" th:remove="none" class="form-control">
	                        <option value="">所有分类</option>
	                        <option th:each="eachValue : ${categorys}" th:value="${eachValue.id}" th:text="${eachValue.name}"></option>
	                    </select>
	                </div> -->
	                
                	<div class="form-group">
			            <select th:field="*{categoryId}"  id="select_goods_CategoryId" name="select_goods_CategoryId"  style="width:250px;display:none;" th:remove="none" class="form-control">
			            	<option value="">所有分类</option>
			                <option style="display:none"  th:each="eachValue : ${categorys}"  th:value="${eachValue.id}"   th:text="${eachValue.name}"></option>
			            </select>
			            <button id="treebutton" class="btn btn-default dropdown-toggle" type="button"  data-toggle="dropdown">
								分类 <i class="fa fa-angle-down"></i>
						</button>
						<div class="dropdown-menu dropdown-content input-large">
							<div id="category_tree" role="menu"></div>
						</div>
							
			            <input type="hidden" id="hidden_categoryid" th:value="${categoryId}" /> 														
					</div>
	
	                <div class="form-group">
	                    <select id="select_goods_BrandsId" th:field="*{brandId}" th:remove="none" class="form-control">
	                        <option value="">所有品牌</option>
	                        <option th:each="eachValue : ${brands}" th:value="${eachValue.id}" th:text="${eachValue.name}"></option>
	                    </select>
	                </div>
	                
	                <div class="form-group">
				        <label class="sr-only" for="goodSn">商品货号</label>
					    <input type="text" name="goodSn" th:field="*{goodSN}"  id="goodSn" style="width:120px;" placeholder="商品货号" class="form-control" />
				    </div> 
	
	                <div class="form-group">
	                    <label class="sr-only" for="input_goodsName">goodsName</label>
	                    <input type="text" th:field="*{goodsName}" placeholder="商品名称"  id="input_goodsName" class="form-control" />
	                </div>
	                <button type="button" id="bt_goods_search" name="bt_goods_search" class="btn green">
	                    <i class="fa  fa-search"></i>
	                </button>
	                <input type="hidden" id="input_search_type" th:field="*{searchType}"/>
	                <input type="hidden" th:field="*{searchId}"/>
	            </form>	          
	        </div>
        </div>
        <div class="col-md-12">
            <div class="form">
                <div class="form-body">
                    <div th:replace="support/searchGoods :: search_list"></div>
                </div>
            </div>
        </div>
   </div>
  <!--  <div style="margin-top: 10px;" class="form-actions fluid">
       <div class="col-md-offset-3 col-md-5">
           <button id="bt_submit" onclick="checkAndSubmit()" class="btn blue btn-block">提交</button>
       </div>
   </div> -->
        
      
</div>
			</div>
		</div>
	</div>
</div> 
</body>
</html>
<script th:src="@{/plugins/jstree/dist/jstree.min.js}" src="plugins/jstree/dist/jstree.min.js" type="text/javascript"></script>
<script th:inline="javascript">
/*<![CDATA[*/
	$("#text_startDate").click(function(){
		WdatePicker({startDate:'%y-%M-01 00:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true,readOnly:true});
	}).focus(function () {
        WdatePicker({startDate:'%y-%M-01 00:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true,readOnly:true});
    });
	$("#text_endDate").click(function(){
		WdatePicker({startDate:'%y-%M-01 00:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true,readOnly:true});
	}).focus(function () {
        WdatePicker({startDate:'%y-%M-01 00:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true,readOnly:true});
    }); 			
		  
   function checkDate(){
	  var startTime=$("#text_startDate").val();
	  var endTime=$("#text_endDate").val(); 
	  var start=new Date(startTime.replace("-", "/").replace("-", "/"));     
	  var end=new Date(endTime.replace("-", "/").replace("-", "/"));  
	  if(end!=""){
		  if(end<=start){  
	        alert("开始时间要小于结束时间"); 
	        $("#text_startDate").val("");
	        $("#text_endDate").val(""); 
	        return;
		  }
      }
  } 

   //包邮类型点击事件
   $("#optionsRadios4").click(function(){
	  $("#hidden_orderPrice").hide();
	  $("#hidden_single_div").show();
	  $("#hidden_brandName").hide();
   });	
   
   $("#optionsRadios5").click(function(){
	 $("#hidden_orderPrice").show();
	 $("#hidden_single_div").hide();
	 $("#hidden_brandName").hide();
   });	
   
   $("#optionsRadios6").click(function(){
		 $("#hidden_orderPrice").hide();
		 $("#hidden_single_div").hide();
		 $("#hidden_brandName").show();
   });	
   

//提交
   $("#btn_form_submit").click(function () {	   	   
	   if($("input[type='radio']:checked").val() == "SINGLEGOOD"){
		   $('#goodsListAdd').val(getSelectedGoodsIds());
	       if(!$('#goodsListAdd').val()){
	           alert("选择要添加的单品");
	           return  false;
	       }	
       }
	  
       $("#form_postForFreeEdit").submit();
   });


//商品分类控制
	    $('#category_tree').jstree({
            "core" : {
                'data' : {'url' : function (node) {return '/tree/category';}}
            }
        });
        
	    $('#category_tree').on('select_node.jstree', function(e,data) { 
	    	checkCat(data.node.id);       
	    });
	    
	    var result;
	    function getCategoryById(id,categories){   	
	    	$(categories).each(function() { 
	    		if(this.id == id)result = this; 
	    		console.log(this.id);
		    	if(result){
		    		console.log('find it' + result);
		    		return result;
		    	}else{
		    		console.log('not find ,try sub cat' + result);
		    		result = getCategoryById(id,this.children);
		    		return result;
		    	}
	    	});
			return result;
	    }
	    
	    function addSubCategoriesToArray(category,array){
	    	//console.log(category.children);
	    	if(category.children){
		    	$(category.children).each(function() {
		    		console.log(this);
		    		array[array.length] = this.id;	 
		    		console.log(this.children);   		
		    		if(this.children)addSubCategoriesToArray(this.children,array);
		    	});
	    	}
	    }
	    
function checkCat(id){

	   $("#select_goods_CategoryId option").each(function() { this.selected = (this.value == id); });
	   $("#select_goods_CategoryId").change();
	   $("#tree_modal").modal('hide');	
}
	           
/*]]>*/
</script>